// wholesale/index.js
const api = require("../request/api")
const request = require("../request/request")
const util = require("../utils/util")
const App = getApp();
const image = 'https://tdesign.gtimg.com/mobile/demos/example2.png';
const items = new Array(12).fill({ label: '标题文字', image }, 0, 12);

// 示例数组
const simple = [
    {
        label: '选项一',
        title: '标题一',
        badgeProps: {},
        items: items.slice(0, 6),
    },
    {
        label: '选项二', // 左侧选项标签
        title: '标题二', // 右侧头顶标题
        badgeProps: {    //徽标 
            dot: true,
        },
        items: [ // 右侧选项列表
            { label: "小柴胡", value: 1 },
            { label: "999感冒", value: 2 },
        ],
    }
]

Page({
    offsetTopList: [],
    /**
     * 页面的初始数据
     */
    data: {
        barHeight: App.globalData.paddingTop,
        // 导航栏数据
        styledata: {
            "bg_color": "#fff",
            "color": "#000",
            "name": "采购下单",
            "leftflag": true,
            // "homeflag": true,
        },

        sideBarIndex: 0,
        scrollTop: 0,
        categories: [],
        navbarHeight: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getCustomNavbarHeight();
        this.get_good_list()
    },

    getCustomNavbarHeight() {
        const query = wx.createSelectorQuery();
        query.select('.custom-navbar').boundingClientRect();
        query.exec((res) => {
            const { height = 0 } = res[0] || {};
            this.setData({ navbarHeight: height });
        });
    },

    onSideBarChange(e) {
        const { value } = e.detail;

        this.setData({ sideBarIndex: value, scrollTop: 0 });
    },

    async get_good_list() {
        util.showLoading("商品加载中")
        let params = {
            page: 1,
            limit: 100,
        }
        let list = this.data.good_list
        //  goods_list接口拿到的是redis缓存内容，查询用goodssearch
        let res = await request.post(api.goods_search, params)
        if (res.length == 0 || !res) {
            this.setData({
                noMore: true
            })
            util.hideLoading()
            util.showToast('暂无更多相关商品')
            return
        }
        let categories = []
        for (var i = 0; i < res.length; i++) {
            let gooditem = {
                label: res[i].name,
                title: "",
                badgeProps: {}
            }

            let product = res[i].type == "default" ? JSON.parse(res[i].spec_array) : JSON.parse(res[i].package_array)
            console.log(product);

            gooditem.items = product.map((item) => {
                let name = item.name + " 单价(元)："
                let price = !item.agency_price ? "点击详查" : item?.agency_price.toFixed(2)
                name = name + price
                return {
                    label: name,
                    gid: res[i].id,
                    pid: item.id,
                    image: res[i].img
                }
            })

            categories.push(gooditem)
        }
        this.setData({
            categories: categories
        })
        util.hideLoading()

    },

    onClickItem(e) {
        let gid = e.currentTarget.dataset.gid
        let pid = e.currentTarget.dataset.pid
        console.log("gid,pid", gid, pid);

        console.log(e);
        wx.navigateTo({
            url: `/wholesale/orderpay?type=1&good_id=${gid}&productID=${pid}&number=1`,
            success: (result) => {
                console.log(result)
            },
            fail: (err) => {
                console.log(err);

            },
            complete: () => { }
        });

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})